<?php


// ***** Config ****************************************************************

$chat_admins    = array('admin');


$chat_histlen   = 1000;


$chat_t_logout  = 9.00;


$chat_t_refresh = 1.00;


$chat_err_inval = 'Invalid username or password!';
$chat_err_inuse = 'The username you selected is already in use!';
$chat_err_kick  = 'You are not allowed to enter   the chat. Contact admin for explanation.';
$chat_err_mute  = 'You are not allowed to post in the chat. Contact admin for explanation.';


function chat_chk($username, $password)
{

  return true;

  // Enter MySQL access information
  $MySQL_username = 'root';
  $MySQL_password = 'pass';
  $MySQL_database = 'phpdevel';

  // Enter the location of user data in MySQL database
  $MySQL_table = 'members';
  $MySQL_username_field = 'username';
  $MySQL_password_field = 'password';

  // The code bellow should work without modification for most users
  mysql_connect('localhost', $MySQL_username, $MySQL_password);
  mysql_select_db($MySQL_database);
  $username = isset($username) ? mysql_real_escape_string($username) : '';
  $password = isset($password) ? mysql_real_escape_string($password) : '';
  $result   = mysql_query("select * from $MySQL_table where $MySQL_username_field = '$username' and ($MySQL_password_field = '$password' or $MySQL_password_field = MD5('$password') or MD5($MySQL_password_field) = '$password' or $MySQL_password_field = MD5(MD5('$password')) or MD5(MD5($MySQL_password_field)) = '$password')");
  return mysql_fetch_assoc($result);
}


// ***** Init ******************************************************************

error_reporting(E_ALL ^ E_NOTICE);
ini_set("log_errors",     0);
ini_set("display_errors", 1);

if (!headers_sent())
{
  session_id  ('lock');
  session_name('lock');
  session_start();

  header("Pragma: no-cache");
  header("Cache-Control: no-cache");
  header("Expires: Fri, 31 Dec 1999 23:59:59 GMT");
}

$chat_data = file_get_contents(dirname(__FILE__) . '/data.txt');
$chat_data = $chat_data ?  unserialize($chat_data) : array();
if (!isset($chat_data['room'])) $chat_data['room'] = array();
if (!isset($chat_data['user'])) $chat_data['user'] = array();
if (!isset($chat_data['pass'])) $chat_data['pass'] = array();
if (!isset($chat_data['data'])) $chat_data['data'] = array();
if (!isset($chat_data['kick'])) $chat_data['kick'] = array();
if (!isset($chat_data['mute'])) $chat_data['mute'] = array();


// ***** Lib *******************************************************************

// ***** microtime_float    *****

function microtime_float()
{
  list($usec, $sec) = explode(' ', microtime());
  return (float)$usec+(float)$sec;
}

// ***** file_put_contents  *****

if (!function_exists('file_put_contents'))
{
  function file_put_contents($name, $data)
  {
    if (!($handle = @fopen($name, 'w'))) return false;
    $bytes = fwrite($handle, $data);
    fclose($handle);
    return $bytes;
  }
}

// ***** strip_slashes_deep *****

if (!function_exists('strip_slashes_deep') && get_magic_quotes_gpc())
{
  function strip_slashes_deep($value)
  {
    if (is_array($value)) return array_map('strip_slashes_deep', $value);
    return stripslashes($value);
  }

  $_GET    = strip_slashes_deep($_GET);
  $_POST   = strip_slashes_deep($_POST);
  $_COOKIE = strip_slashes_deep($_COOKIE);
}

// ***** unlog_users        *****

function unlog_users()
{
  $modified = false;

  if (isset($GLOBALS['chat_data']['user'][$_GET['user']]))
  {
    $GLOBALS['chat_data']['user'][$_GET['user']] = microtime_float();
    $modified = true;
  }

  foreach  ($GLOBALS['chat_data']['user'] as $user => $tm)
  if ($tm  <  microtime_float()-$GLOBALS['chat_t_logout'])
  {
    $GLOBALS['chat_data']['data'][] = "-\r\n{$GLOBALS['chat_data']['room'][$user]}\r\n$user";
    unset($GLOBALS['chat_data']['room'][$user]);
    unset($GLOBALS['chat_data']['user'][$user]);
    unset($GLOBALS['chat_data']['pass'][$user]);
    $modified = true;
  }

  foreach  ($GLOBALS['chat_data']['kick'] as $user => $tm) if ($tm < time())
  {
    unset($GLOBALS['chat_data']['kick'][$user]);
    $modified = true;
  }

  foreach  ($GLOBALS['chat_data']['mute'] as $user => $tm) if ($tm < time())
  {
    unset($GLOBALS['chat_data']['mute'][$user]);
    $modified = true;
  }

  return  $modified;
}

?>